#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   check_label_has_price.py    
@Contact :   fengfeng.qiu@amh-group.com

@Modify Time      @Author    
------------      -------    
2022/8/24 16:08   qiufengfeng      
'''
import ast


from qiu_utils.db_utils.mysql_utls import MysqlConnector
from price_extra import price_extract
MYSQL_INFO = {
    'host':"10.13.68.93",
    "port":3306,
    "user":"root",
    'passwd':"123456",
    'db':"contraband",
    "charset":"utf8"
}
table_name = "biaozhu_price_valid"
mysql_instance = MysqlConnector(MYSQL_INFO,table_name)

def delete_label_has_no_target_price():
    '''
    将标注里面的，没有价格的给pass或者不是到车价，总价，运费的给pass
    :return:
    '''
    mysql_instance = MysqlConnector(MYSQL_INFO, table_name)

    label_df = mysql_instance.select(['id','text','label'],{})
    label_df = label_df[label_df['label'] != ""]
    label_df['doccano_index'] = list(range(label_df.shape[0]))
    def update_label(item):
        label = item['label']
        text = item['text']
        if label != "":
            if type(label) == str:
                label = ast.literal_eval(label)

            filter_new_label = []
            for l in label:
                (start,end,label_type) = l
                extract_price_infos = price_extract(text[start:end])

                is_valid_label = False
                for unit_p in extract_price_infos:
                    if unit_p["price_text"] != "" or unit_p["price_text"] != None:
                        if unit_p["price_type"] == None:
                            is_valid_label = True
                            break
                        if (unit_p["price_type"] != None or unit_p["price_type"] != "") and unit_p["price_type"]  in ['到车费','运费','总价','单价',"加价"]:
                            is_valid_label = True
                            break

                if is_valid_label:
                    filter_new_label.append(l)
                else:
                    print("index:{index} ,text: {text}".format(index=item['doccano_index'],text=text[start:end]))


            return filter_new_label
        else:
            return []

    label_df['label'] = label_df.apply(update_label,axis=1)


    def apply_update(item,mysql_instance):
        label = item['label']
        mysql_id = item['id']

        mysql_instance.update( {'label':label}, {"id":str(mysql_id)})

    label_df.apply(apply_update,axis=1,args=(mysql_instance,))

if __name__ == '__main__':
    delete_label_has_no_target_price()




